*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do


tempname results
postfile `results' coef_0 coef_1 coef_2 coef_3 coef_4 coefT_0 coefT_1 coefT_2 coefT_3 coefT_4 rmse_0 rmse_1 rmse_2 rmse_3 rmse_4 rmseT_0 rmseT_1 rmseT_2 rmseT_3 rmseT_4 using "`outfigdir'`s'figure7_data.dta", replace



forval h=1/500 {
* Load stacked pair data at the county or czone-state level
use "`countydir'`s'countypairtypes_years.dta", clear

***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************



preserve
			collapse (first) mw, by(state year)
			gen statemin=state
			gen mwmin=mw
			keep statemin mwmin year
			tempfile a1
			save `a1', replace
restore


preserve
			collapse (first) mw, by(state year)
			gen statemax=state
			gen mwmax=mw
			keep statemax mwmax year
			tempfile a2
			save `a2', replace
restore



preserve
			collapse (min) statemin=state (max) statemax=state, by(countypair year)
			egen stateminmaxg=group(statemin statemax)
			collapse (first) statemin statemax, by(stateminmaxg year)
			bys stateminmaxg: gen byspys=rnormal(0,0.13)
			merge m:1 statemin year using "`a1'"
			keep if _merge==3
			drop _merge
			merge m:1 statemax year using "`a2'"
			keep if _merge==3
			drop _merge
			gen mw=(mwmin+mwmax)/2
			gen lmw=log(mw)
			tsset stateminmaxg year
			gen dlmw=D.lmw
			gen sd_byspys=0
			gen mean_byspys=0
			gen sd_dlmw=0
			gen mean_dlmw=0
			gen byspymws=0
			forval j=1/106 {
			summarize byspys if stateminmaxg==`j'
			replace sd_byspys=r(sd)
			replace mean_byspys =r(mean)
			summarize dlmw if stateminmaxg==`j'
			replace sd_dlmw=r(sd)
			replace mean_dlmw=r(mean)
			*Correlation between min wage changes and czonepair-year shocks
			local rho -0.45
			replace byspymws = `rho' * (sd_byspys/sd_dlmw) * (dlmw - mean_dlmw) + ((1 - `rho'^2)^0.5) * (byspys - mean_byspys) + mean_byspys if stateminmaxg==`j'
			}
			keep statemin statemax year byspys byspymws
			tempfile a3
			save `a3', replace
restore



preserve
			collapse (first) mw, by(czone state year)
			collapse (mean) mw, by(czone year)
			gen lmw=log(mw)
			bys czone: gen byczys=rnormal(0,0.17)
			egen czoneg=group(czone)
			tsset czone year
			gen dlmw=D.lmw
			gen sd_byczys=0
			gen mean_byczys=0
			gen sd_dlmw=0
			gen mean_dlmw=0
			gen byczymws=0
			forval j=1/424 {
			summarize byczys if czoneg==`j'
			replace sd_byczys=r(sd)
			replace mean_byczys =r(mean)
			summarize dlmw if czoneg==`j'
			replace sd_dlmw=r(sd)
			replace mean_dlmw=r(mean)
			*Correlation between min wage changes and czone-year shocks
			local rho 0.2
			replace byczymws = `rho' * (sd_byczys/sd_dlmw) * (dlmw - mean_dlmw) + ((1 - `rho'^2)^0.5) * (byczys - mean_byczys) + mean_byczys if czoneg==`j'
			}
			keep czone year byczys byczymws
			tempfile a4
			save `a4', replace

restore


preserve

			collapse (first) region, by(county)
			drop region
			gen countyfe=runiform(0.3,12.14)
			tempfile bbb
			save `bbb', replace
			
restore


preserve

			keep county countypair year
			gen eps=rnormal(0,0.2)
			tempfile ddd
			save `ddd', replace
restore


*** use drawnorm to create correlated shocks

bys countypair year: egen statemin=min(state)
bys countypair year: egen statemax=max(state)

merge m:1 statemin statemax year using "`a3'"
drop _merge


merge m:1 czone year using "`a4'"
drop _merge


merge m:1 county using "`bbb'"
drop _merge

merge m:1 county countypair year using "`ddd'"
drop _merge

gen lmw=log(mw)


*State shock neg corr with mw changes
gen statepshock=byspymws

*Czone shock pos corr with mw changes
gen czshock=byczymws

*Generate logemp data
local mwelast -0.2	

gen lempp= countyfe+czshock+statepshock+`mwelast'*lmw+eps

preserve

			collapse (mean) lemp=lempp, by(county year)
			tempfile lempf
			save `lempf', replace
restore

merge m:1 county year using "`lempf'"
drop _merge


bys countypair_id: egen state1=min(state)
bys countypair_id: egen state2=max(state)
egen bordersegment = group(state1 state2)
egen pairperiod=group(countypair  year)
egen czonestate=group(czone state)

local options1 "vce(cluster state bordersegment) absorb(county pairperiod)"
local options2 "vce(cluster state) absorb(county year)"


forval j=0/4  {
reghdfe lempp lmw if type`j'==1, `options1'
scalar coef_`j' = _b[lmw]  
scalar rmse_`j' = e(rmse)

reghdfe lempp lmw if typec`j'==1, `options2'
scalar coefT_`j' = _b[lmw]
scalar rmseT_`j' = e(rmse)    
}

    * Post results to the postfile
    post `results' (coef_0) (coef_1) (coef_2) (coef_3) (coef_4) (coefT_0) (coefT_1) (coefT_2) (coefT_3) (coefT_4) (rmse_0) (rmse_1) (rmse_2) (rmse_3) (rmse_4) (rmseT_0) (rmseT_1) (rmseT_2) (rmseT_3) (rmseT_4)
	
}


postclose `results'

use "`outfigdir'`s'figure7_data.dta", clear

forval j=0/4  {
ren coef_`j' type`j'
ren coefT_`j' twfe`j'
ren rmse_`j' rmse`j'
ren rmseT_`j' rmseT`j'
}

twoway (kdensity twfe0, lc(black) lw(0.5)) (kdensity twfe1,  lc(red) lp(dash) lw(0.5)) (kdensity twfe2, lc(blue) lp(shortdash) lw(0.5)) (kdensity twfe3, lc(blue*.6) lp(dash_dot) lw(0.5)) (kdensity twfe4, lc(blue*.3) lp(longdash) lw(0.5)), xline(-0.2, lc(black) lw(0.5))  scheme(s1color) xlabel(-0.6 -0.5 -0.4 -0.3 -.2 -0.1 0) ylabel(0 2 4 6) xtitle("") legend(order(1 "Contiguous pairs" 2 "Subsample 1" 3 "Subsample 2" 4 "Subsample 3" 5 "Subsample 4"))
gr export "`outfigdir'`s'figure7L.eps", replace

twoway (kdensity type0, lc(black) lw(0.5)) (kdensity type1,  lc(red) lp(dash) lw(0.5)) (kdensity type2, lc(blue) lp(shortdash) lw(0.5)) (kdensity type3, lc(blue*.6) lp(dash_dot) lw(0.5)) (kdensity type4, lc(blue*.3) lp(longdash) lw(0.5)), xline(-0.2, lc(black) lw(0.5))  scheme(s1color) xlabel(-0.3 -0.2 -0.1 0 0.1) ylabel(0 4 8 12) xtitle("") legend(order(1 "Contiguous pairs" 2 "Subsample 1" 3 "Subsample 2" 4 "Subsample 3" 5 "Subsample 4"))
gr export "`outfigdir'`s'figure7R.eps", replace

clear all
